package com.my.gesturelockandwakedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.gesturelockandwakedemo.MainActivity;
import com.my.gesturelockandwakedemo.R;
import com.my.gesturelockandwakedemo.util.CacheActivity;

public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener {
    private Handler handler = new Handler();
    /*** 跳过 */
    private RelativeLayout rlJumpOver;
    private TextView tvSecond;
    /*** 倒计时 */
    private int mCount = 3;
    /*** 倒计时方法 */
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        countDownTimer.start();
    }

    //    @Override
    private void initViews() {
//        super.initViews();
        rlJumpOver = (RelativeLayout) findViewById(R.id.rl_jump_over_welcome);
        rlJumpOver.setOnClickListener(this);
        tvSecond = (TextView) findViewById(R.id.tv_second_welcome);
        goTimer();
    }


    /**
     * 运行倒计时
     */
    private void goTimer() {
        countDownTimer = new CountDownTimer(mCount * 1000 + 1050, 1000) {
            @Override
            public void onTick(long l) {
                tvSecond.setText("" + (int) (l / 1000 - 1));
            }

            @Override
            public void onFinish() {
                goIntent();
            }
        };
        countDownTimer.start();
    }

    /**
     * 跳转页面
     */
    private void goIntent() {
        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
        startActivity(intent);
        CacheActivity.finishSingleActivity(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        countDownTimer.cancel();
        Log.i("mwbm", "onStop");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_jump_over_welcome: // 跳过
                countDownTimer.cancel();
                goIntent();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            CacheActivity.finishActivity();
//            if ((System.currentTimeMillis() - mExitTime) > 2000) {
//                //   Object mHelperUtils;
//                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
//                mExitTime = System.currentTimeMillis();
//            } else {
//                CacheActivity.finishActivity();
//            }
//            return true;
        }
//        else if (keyCode == KeyEvent.KEYCODE_HOME) {
//            CacheActivity.finishActivity();
//        }
        return super.onKeyDown(keyCode, event);
    }
}
